/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.playground.client.websockets.util;

import com.google.gson.Gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import com.playground.client.websockets.entity.message.Action;
import com.playground.client.websockets.entity.message.request.MakeMoveRequestMessage;
import com.playground.client.websockets.entity.message.request.EnterQueueRequestMesage;
import com.playground.client.websockets.entity.message.request.LoginRequestMessage;
import com.playground.client.websockets.entity.message.request.PlayerRegistrationRequestMessage;
import com.playground.client.websockets.entity.message.request.RequestMessage;
import com.playground.client.websockets.entity.message.request.RoomRegistrationRequestMessage;
import com.playground.client.websockets.entity.message.request.WriteToChatRequestMessage;
import java.lang.reflect.Type;
import org.jboss.logging.Logger;

/**
 *
 * @author Tiago Peres
 */
public class RequestMessageDeserializer implements JsonDeserializer<RequestMessage> {

    @Override
    public RequestMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context){
        System.out.println("Desirializing...");
        String error;
        JsonElement jsonAction = json.getAsJsonObject().get("action");
        if (null != jsonAction){
            try{
                Action action = Action.valueOf(jsonAction.getAsString());
                Gson gson = new Gson();
                switch(action){
                    case login:
                        return gson.fromJson(json, LoginRequestMessage.class);
                    case registerPlayerToSocket:
                        System.out.println("action is registerPlayerToSocket...");
                        PlayerRegistrationRequestMessage rmsg = gson.fromJson(json, PlayerRegistrationRequestMessage.class);
                        System.out.println("playerId: " + rmsg.getPlayerId());
                        System.out.println("playerId: " + rmsg.getPlayerKey());
                        System.out.println("message was succesfully desirialized as a PlayerRegistrationRequestMessage.");
                        return rmsg;
                    case enterQueue:
                        return gson.fromJson(json, EnterQueueRequestMesage.class);
                    case registerRoomToSocket:
                        return gson.fromJson(json, RoomRegistrationRequestMessage.class);
                    case writeToChat:
                        return gson.fromJson(json, WriteToChatRequestMessage.class);
                    case makeMove:
                        return gson.fromJson(json, MakeMoveRequestMessage.class);
                    default:
                        RequestMessage msg = new RequestMessage();
                        msg.setAction(action);
                        String log = "'" + action + "' is valid, but no specific parser was declared for it. '" + action + "' was parsed as a simple RequestMessage.";
                        Logger.getLogger(RequestMessageDeserializer.class).log(Logger.Level.WARN, log);
                        return msg;
                }
            } catch(IllegalArgumentException e){
                error = "'" + jsonAction.getAsString() + "' is not valid.";
                Logger.getLogger(RequestMessageDeserializer.class).log(Logger.Level.WARN, error);
            }
        } else{
            error = "invalid message, json must have an 'action' attribute.";
            Logger.getLogger(RequestMessageDeserializer.class).log(Logger.Level.WARN, error);
        }
        throw new JsonSyntaxException(error);
    }
    
}
